package com.cn.wanxi.autoconfig;

import com.cn.wanxi.autoconfig.config.SelfDefineConfig;
import com.cn.wanxi.autoconfig.itf.EnableSelfDefineConfig;
import com.cn.wanxi.autoconfig.itf.EnableSelfDefineConfig2;
import com.cn.wanxi.autoconfig.itf.EnableSelfDefineConfig3;
import com.cn.wanxi.autoconfig.service.IUserService;
import com.cn.wanxi.autoconfig.service.impl.UserServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.SpringFactoriesLoader;



@EnableAspectJAutoProxy //开启aop
@SpringBootApplication
@Log4j2

//region 测5
@EnableSelfDefineConfig3
//endregion


//region 测4

//@EnableSelfDefineConfig2

//endregion

//region 测3
//@EnableSelfDefineConfig
//endregion

//region 测2
//@Import({SelfDefineConfig.class})
//endregion

//region 测1
//使用import方式加载UserServiceImpl的bean到容器
//@Import({UserServiceImpl.class})
//endregion

public class AutoconfigApplication {

    public static void main(String[] args) {

        ConfigurableApplicationContext app = SpringApplication.run(AutoconfigApplication.class, args);
        SpringFactoriesLoader a;


        //region 5.通过@Enablexxx与实现ImportSelector动态读取MATE-INF里面的spring.factories的方式加载bean
        try {

            IUserService userService = (IUserService) app.getBean("userService");
            userService.getUserById(5);
        } catch (Exception e) {

            log.error("加载bean出现错误！原因如下：\r\n{}", e.getMessage());
        }
        //endregion

        //region 4.通过@Enablexxx与实现ImportSelector的方式加载bean
//        try{
//
//            IUserService userService = (IUserService) app.getBean("userService");
//            userService.getUserById(4);
//        }catch (Exception e){
//
//            log.error("加载bean出现错误！原因如下：\r\n{}",e.getMessage());
//        }
        //endregion


        //region 3.使用@EnableXXX的方式加载配置类

//        try{
//
//            IUserService userService = (IUserService) app.getBean("userService");
//
//            userService.getUserById(3);
//        }catch (Exception e){
//
//            log.error("加载bean出现错误！原因如下：\r\n{}",e.getMessage());
//        }


        //endregion


        //region 2.使用Import 通过加载配置类的方式加载bean的测试

//        try{
//
//        IUserService userService = (IUserService) app.getBean("userService");
//
//        userService.getUserById(2);
//        }catch (Exception e){
//
//            log.error("加载bean出现错误！原因如下：\r\n{}",e.getMessage());
//        }


        //endregion


        //region 1.@Import 直接加载bean的测试
//        try {
//            IUserService userService = app.getBean(IUserService.class);
//
//            userService.getUserById(1);
//        } catch (NoSuchBeanDefinitionException exception) {
//            log.error("找不到 IUserService 所对应的实体类bean");
//        }
        //endregion


    }

}
